﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.ApplicationModel.Core;
using Windows.Data.Json;
using Xiaowei.Services;

namespace Xiaowei.ResponseHandlers
{
    internal static class OpenAppResponseHandler
    {
        public static void Handle(IResponseData responseData)
        {
            if (responseData is IOpenAppResponseData openAppResponseData)
            {
                switch (openAppResponseData.AppType)
                {
                    case AppType.App:
                        string appName = openAppResponseData.AppName;
                        if (!ServiceFactory.OpenAppService.OpenApp(appName))
                        {
                            HttpClient httpClient = new HttpClient();
                            try
                            {
                                HttpResponseMessage responseMessage = httpClient.GetAsync(new Uri("https://s.pcmgr.qq.com/tapi/web/searchcgi.php?type=hp&keyword=" + appName + "&page=1&pernum=1&more=0")).Result;
                                if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                                {
                                    string s = responseMessage.Content.ReadAsStringAsync().Result;
                                    JsonObject jo2 = JsonObject.Parse(s);
                                    //{"code":0,"subcode":0,"message":"ok","default":0,"info":{"total":1},"data":{"pc":{"app":[{"appid":"11488","detailurl":"https:\/\/pc.qq.com\/detail\/8\/detail_11488.html","name":"\u5fae\u4fe1\u7535\u8111\u7248"}]}}}
                                    JsonObject data = jo2.GetNamedObject("data");
                                    JsonObject pc = data.GetNamedObject("pc");
                                    JsonArray apps = pc.GetNamedArray("app");
                                    JsonObject app = apps[0].GetObject();
                                    string url = app.GetNamedString("detailurl").Replace("\\", "");
                                    _ = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                                        _ = Windows.System.Launcher.LaunchUriAsync(new Uri(url));
                                    });
                                    responseData.ResponseText= "未安装此应用，正在转到安装页面。";
                                    responseData.TTSUrl = XiaoweiAI.Instance.RequestTTS("未安装此应用，正在转到安装页面。");
                                    break;
                                }
                            }
                            catch
                            {

                            }
                            finally
                            {
                                httpClient.Dispose();
                            }
                            responseData.ResponseText = "打开失败,请检查是否安装或尝试重启小微app";
                            responseData.TTSUrl = XiaoweiAI.Instance.RequestTTS("打开失败,请检查是否安装或尝试重启小微app");
                        }
                        break;
                    case AppType.SettingPage:
                        if (UriDic.ContainsKey(openAppResponseData.AppName))
                        {
                            var t = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                () => {
                                    var t2 = Windows.System.Launcher.LaunchUriAsync(new Uri(UriDic[openAppResponseData.AppName]));
                                }
                                );
                        }
                        break;
                    default:
                        break;
                }
            }
            DefaultResponseHandler.Handle(responseData);
        }

        private static Dictionary<string, string> UriDic { get; set; }
            = new Dictionary<string, string>()
            {
                {"账户","ms-settings:emailandaccounts" },
                {"登录","ms-settings:signinoptions" },
                {"应用","ms-settings:appsfeatures" },
                {"蓝牙","ms-settings:bluetooth" },
                {"设备","ms-settings:connecteddevices" },
                {"打印机","ms-settings:printers" },
                {"显示","ms-settings:easeofaccess-display" },
                {"代理","ms-settings:network-proxy" },
                {"VPN","ms-settings:network-vpn" },
                {"vpn","ms-settings:network-vpn" },
                {"无线网络","ms-settings:network-wifi" },
                {"显示器","ms-settings:screenrotation" },
                {"电源","ms-settings:powersleep" },
                {"日期","ms-settings:dateandtime" },
                {"区域","ms-settings:regionformatting" },
                {"语言","ms-settings:regionlanguage" },
                {"系统更新","ms-settings:windowsupdate" },
                {"安全管理","ms-settings:windowsdefender" },
            };
    }
}
